package cn.e2win.biz.cyzj;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.e2win.base.entity.SysLabel;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.base.json.vo.WeixinUser;
import cn.e2win.biz.activity.ActivityService;
import cn.e2win.biz.annotation.PageParam;
import cn.e2win.biz.base.BaseService;
import cn.e2win.biz.base.CommonService;
import cn.e2win.biz.dbo.CyzjOrg;
import cn.e2win.biz.util.StringUtils;


@Controller
@Scope("request")
@RequestMapping("/m")
public class CyzjControllerM extends CyzjController {

	@Autowired
	private CyzjService cyzjService;
	@Autowired
	private CommonService commonService;
	@Autowired
	private ActivityService activityService;

	@Override
	public BaseService initService() {
		return cyzjService;
	}

	@RequestMapping("cyzjMain")
	public String cyzjMain(HttpServletRequest request, @ModelAttribute CyzjOrg cyzjOrg, ModelMap modelMap){
		pager.getScMap().put("approveStatus", "APPROVED");

		List<SysLabel> labelMainList = commonService.getSysLabelList(CyzjOrg.class.getSimpleName(), "labelMain");
		modelMap.addAttribute("labelMainList",labelMainList);
		

		try {
			if(!StringUtils.isEmpty(cyzjOrg.getLabelMain())){
				cyzjOrg.setLabelMain(new String(cyzjOrg.getLabelMain().getBytes("iso8859-1"),"utf-8"));
				pager.getScMap().put("LabelMain_L",cyzjOrg.getLabelMain());
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		if(!StringUtils.isEmpty(cyzjOrg.getAreaId())){
			pager.getScMap().put("areaId",cyzjOrg.getAreaId()+"");
		}
		pager.getScMap().put("approveStatus", "APPROVED");
		cyzjService.pagerCyzj(pager);
		modelMap.put("cyzjPager", pager);
		


		/*Pager activityPager = new Pager();
		activityPager.setCurrUser(pager.getCurrUser());
		activityPager.getScMap().put("approveStatus", "APPROVED");
		activityPager.getScMap().put("processStatus", "running");
		activityService.pagerActivity(activityPager);
		modelMap.put("activityPager", activityPager);*/
		return "m/cyzjMain";
	}

	@RequestMapping("myCyzj")
	public String myCyzj(HttpServletRequest request, ModelMap modelMap){
		//我的入驻
		Pager joinPager = new Pager();
		joinPager.getScMap().put("approveStatus", "APPROVED");
		joinPager.getScMap().put("user_I","join");
		joinPager.setCurrUser(pager.getCurrUser());
		cyzjService.pagerCyzj(joinPager);
		modelMap.put("joinPager", joinPager);
		//我的发起
		Pager createPager = new Pager();
		createPager.getScMap().put("approveStatus", "APPROVED");
		createPager.getScMap().put("user_I","master");
		createPager.setCurrUser(pager.getCurrUser());
		cyzjService.pagerCyzj(createPager);
		modelMap.put("createPager", createPager);
		return "m/myCyzj";
	}

	@RequestMapping("cyzjPagerM")
	@ResponseBody
	public Pager cyzjPagerM(HttpServletRequest request, ModelMap modelMap){
		WeixinUser weixinUser =(WeixinUser)request.getSession().getAttribute("weixinUser");
		if(weixinUser!=null){
			pager.getScMap().put("x_I", weixinUser.getBaiduX());
			pager.getScMap().put("y_I", weixinUser.getBaiduY());
		}
		if(StringUtils.isEmpty(pager.getScMap().get("approveStatus"))) {
			pager.getScMap().put("approveStatus", "APPROVED");
		}
		super.cyzjPager(request, modelMap);
		return pager;
	}

	@RequestMapping("/cyzjDetail")
	public String cyzjDetail(HttpServletRequest request, @RequestParam("id")Integer id,
							 ModelMap modelMap) {
		super.toCyzjEditor(request, id, modelMap);
		if(!StringUtils.isEmpty(id)) {
			Pager activityPager = new Pager();
			activityPager.setNumPerPage(3);
			activityPager.getScMap().put("approveStatus","APPROVED");
			activityPager.getScMap().put("orgId", id + "");
			modelMap.addAttribute("activityPager",activityPager);
		}
		Map<String, String> param = new HashMap<String, String>();
		WeixinUser weixinUser =(WeixinUser)request.getSession().getAttribute("weixinUser");
		if(weixinUser!=null){
			param.put("x_I", weixinUser.getBaiduX());
			param.put("y_I", weixinUser.getBaiduY());
		}
		modelMap.put("cyzjOrg",cyzjService.getCyzjOrgDetail(id, param));
		return "/m/cyzjDetail";
	}
	
	@RequestMapping("placeTheMap")
	public String placeTheMap(HttpServletRequest request,
			@PageParam Pager pager, ModelMap modelMap){
		modelMap.put("x", request.getParameter("x"));
		modelMap.put("y", request.getParameter("y"));
		return "/m/theMap";
	}
	
	@RequestMapping("getWeixinCoord")
	@ResponseBody
	public Map<String ,Object> getWeixinCoord(HttpServletRequest request,
			@PageParam Pager pager, ModelMap modelMap){
		WeixinUser weixinUser =(WeixinUser)request.getSession().getAttribute("weixinUser"); 
		String url = "http://api.map.baidu.com/geoconv/v1/?coords="+114.286240+","+30.391621+"&from=3&to=5&ak=Bp7W7v3ueD02INZO3yXquaPKBOn9Vynz";
		Map<String ,Object> jsonObject = sendGet(url, null);
		List<Map<String ,Object>> result=(List<Map<String ,Object>>)jsonObject.get("result");
		
		return result.get(0);
	}
	
	

}
